<?php

namespace Acme\Likipe\BlogBundle\Controller;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Acme\Likipe\BlogBundle\Repository;

use Symfony\Component\HttpFoundation\RedirectResponse;

// Form
use Acme\Likipe\BlogBundle\Form\PostType;

// Entity
use Acme\Likipe\BlogBundle\Entity\Blog;
use Acme\Likipe\BlogBundle\Entity\Post;


class PostController extends Controller
{
    
    public function indexAction( $iId )
    {
    	$this->get( 'session' )
    		->setFlash( 'menu', '2' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oBlog = $oEm->getRepository( 'AcmeLikipeBlogBundle:Blog' )
    		->find( $iId );
    	
    	if ($oBlog == null){
    		 throw $this
    		 	->createNotFoundException( 'No found Blog by id ' . $iId . '!' );
    	}
    	
    	$aPosts = $oBlog->getPosts();
    	
        return $this->render( 'AcmeLikipeBlogBundle:Post:index.html.twig', 
        		array( 'posts' => $aPosts, 'blog' => $oBlog ));
    }
    
    public function addAction( $iId, Request $oRequest ){
    	$this->get( 'session' )
    		->setFlash( 'menu', '2' );
    	
    	$em = $this->getDoctrine()->getEntityManager();
    	 
    	$oBlog = $em->getRepository( 'AcmeLikipeBlogBundle:Blog' )
    	->find($iId);
    	 
    	if ($oBlog == null){
    		throw $this->createNotFoundException( 'No found Blog by id '
    				. $iId . '!' );
    	}
    	
    	$oForm = $this->createForm(new PostType(), new Post());
    	
    	if ($oRequest->getMethod() == 'POST'){
    		$oForm->bindRequest($oRequest);
    		
    		if ($oForm->isValid()){
    			$oPost = new Post();
    			$oPost = $oForm->getData();
    			$oPost->setCreated(new \DateTime( 'now' ));
    			$oPost->setBlog( $oBlog );
    			
    			$em->persist( $oPost );
    			$em->flush();
    			
    			$this->get( 'session' )
    				->setFlash( 'notice', 'Add new Post success!' );
    			
    			return new RedirectResponse( $this
    						->generateUrl( 'AcmeLikipeBlogBundle_post', array(
    								'iId'	=> $iId
    							)));
    		}
    	}
    	
    	return $this->render( 'AcmeLikipeBlogBundle:Post:add.html.twig',
    			array( 'form' => $oForm->createView(),
    					'blog' => $oBlog ));
    }
    
    public function deleteAction( $iId ){
    	$this->get( 'session' )
    		->setFlash('menu', '2');
    		
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oPost = $oEm->getRepository( 'AcmeLikipeBlogBundle:Post' )->find($iId);
    	 
    	if ( $oPost == null ){
    		throw $this
    			->createNotFoundException( 'No found Post by id ' . $iId . '!' );
    	
    	}else{  
    		$oBlog = $oPost->getBlog();
    		
    		foreach ( $oPost->getComments() as $cComment ){
    			$oEm->remove( $cComment );
    		}
    		
	    	$oEm->remove( $oPost );
	    	$oEm->flush();
	    	 
	    	$this->get('session')
	    		->setFlash('notice', 'Delete Post with id ' . 
	    				$iId . ' success!');
    	}
    	 
    	return new RedirectResponse( $this
    			->generateUrl( 'AcmeLikipeBlogBundle_post', array(
    					'iId'	=> $oBlog->getId()
    				)));
    }
    
    public function editAction( $iId, Request $oRequest ){
    	$this->get( 'session' )->setFlash( 'menu', '2' );
    	
    	$oEm = $this->getDoctrine()->getEntityManager();
    	$oPost = $oEm->getRepository( 'AcmeLikipeBlogBundle:Post' )
    		->find( $iId );
    	
    	if ($oPost == null){
    		throw $this
    			->createNotFoundException( 'No found Post by id ' . $iId . '!' );
    	}
    	
    	$oBlog = $oPost->getBlog();
    	
    	$oForm = $this->createForm( new PostType(), $oPost );
    	
    	if ( $oRequest->getMethod() == 'POST' ){
    		$oForm->bindRequest( $oRequest );
    		
    		$oPost = $oForm;
    		
    		$oEm->flush();
    		
    		$this->get( 'session' )
    		->setFlash( 'notice', 'Edit Post with id ' . $iId . ' success!' );
    		
    		return new RedirectResponse( $this
    				->generateUrl( 'AcmeLikipeBlogBundle_post', array(
    						'iId'	=> $oBlog->getId()
    				)));
    	}
    	 
    	return $this->render( 'AcmeLikipeBlogBundle:Post:edit.html.twig',
    			array( 'form' => $oForm->createView(),
    					'post' => $oPost ));
    }
}
